<template>
  <div class="customer-container layout-padding">
    <el-card shadow="hover" class="layout-padding-auto">
      <div class="customer-search mb15">
        <el-form ref="searchFormRef" :model="searchForm" inline>
          <el-form-item label="姓名" prop="name">
            <el-input v-model="searchForm.name" placeholder="请输入"> </el-input>
          </el-form-item>
          <el-form-item label="手机号" prop="mobile">
            <el-input v-model="searchForm.mobile" placeholder="请输入"> </el-input>
          </el-form-item>
          <el-form-item>
            <el-button size="default" type="primary" @click="refresh">
              <el-icon>
                <ele-Search />
              </el-icon>
              查询
            </el-button>
            <el-button size="default" @click="onRest">
              <el-icon>
                <ele-RefreshRight />
              </el-icon>
              重置
            </el-button>
          </el-form-item>
        </el-form>
        <div class="table-tool-bar">
          <el-button size="default" type="primary" v-if="$can('create', 'Customer')" @click="handleAdd()">
            <el-icon>
              <ele-Plus />
            </el-icon>
            新增客户
          </el-button>
        </div>
      </div>
      <el-table :data="tableData.list" v-loading="loading" style="width: 100%">
        <el-table-column type="index" label="序号" width="60" />
        <el-table-column prop="name" label="姓名" show-overflow-tooltip></el-table-column>
        <el-table-column prop="role.name" label="系统角色" show-overflow-tooltip></el-table-column>
        <el-table-column prop="work_role" label="工作角色" show-overflow-tooltip>
          <template #default="scope">
            {{ WorkRoleLabel[scope.row.work_role] }}
          </template>
        </el-table-column>
        <el-table-column prop="mobile" label="手机号" show-overflow-tooltip></el-table-column>
        <el-table-column prop="email" label="邮箱" show-overflow-tooltip></el-table-column>
        <el-table-column prop="gender" label="性别" show-overflow-tooltip>
          <template #default="scope">
            {{ GenderLabel[scope.row.gender] }}
          </template>
        </el-table-column>
        <el-table-column prop="address" label="地址" show-overflow-tooltip>
          <template #default="scope">
            {{ scope.row.area_names?.join('') + scope.row.address }}
          </template>
        </el-table-column>
        <el-table-column prop="username" label="账户名称" show-overflow-tooltip></el-table-column>
        <el-table-column prop="status" label="用户状态" show-overflow-tooltip>
          <template #default="scope">
            <el-tag type="success" v-if="!scope.row.disabled" size="small">启用</el-tag>
            <el-tag type="info" v-else size="small">禁用</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="created_at" label="创建时间" show-overflow-tooltip></el-table-column>
        <el-table-column label="操作" width="100">
          <template #default="scope">
            <el-button
              v-if="$can('update', 'Customer')"
              :disabled="scope.row.super_admin"
              size="small"
              text
              type="primary"
              @click="handleEdit(scope.row)"
              >编辑</el-button
            >
            <el-button
              v-if="$can('delete', 'Customer')"
              :disabled="scope.row.super_admin"
              size="small"
              text
              type="primary"
              @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <CommonPagination :pagination="pagination" :total="tableData.total"></CommonPagination>
    </el-card>
    <UserDialog ref="userDialogRef" @refresh="refresh" />
  </div>
</template>

<script setup lang="ts" name="customer">
import { defineAsyncComponent, reactive, ref } from 'vue';
import { ElMessageBox, ElMessage, FormInstance } from 'element-plus';
import { readAllUser, deleteUser } from '/@/api/user';
import { useTable } from '/@/hooks/use-table';
import CommonPagination from '/@/components/common-pagination/index.vue';
import { User } from '/@/models';
import { DialogType } from '/@/constants/dialog-type';
import { GenderLabel, WorkRoleLabel } from '/@/constants';

// 引入组件
const UserDialog = defineAsyncComponent(() => import('/@/views/system/user/dialog.vue'));

// 定义变量内容
const userDialogRef = ref<InstanceType<typeof UserDialog>>();

const searchFormRef = ref<FormInstance>();
const searchForm = reactive({
  name: '',
  mobile: '',
});

const { tableData, loading, pagination, refresh } = useTable<User>(({ page, pageSize }) =>
  readAllUser({ page, pageSize, ...searchForm }).then(({ data }) => {
    return {
      total: data?.total,
      list: data?.list,
    };
  })
);

const onRest = () => {
  searchFormRef.value?.resetFields();
  refresh();
};

// 打开新增字典弹窗
const handleAdd = () => {
  userDialogRef.value?.openDialog(DialogType.CREATE);
};

// 打开修改字典弹窗
const handleEdit = (row: User) => {
  userDialogRef.value?.openDialog(DialogType.EDIT, row);
};

// 删除字典
const handleDelete = (row: User) => {
  ElMessageBox.confirm(`此操作不可恢复，是否继续?`, '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
    buttonSize: 'default',
    beforeClose: async (action, instance, done) => {
      if (action === 'confirm') {
        instance.confirmButtonLoading = true;
        instance.confirmButtonText = '提交中';
        deleteUser(row.id).then(() => {
          ElMessage.success('删除成功');
          refresh();
        });
        instance.confirmButtonLoading = false;
        done();
      } else {
        done();
      }
    },
  });
};
</script>

<style scoped lang="scss">
.customer-container {
  :deep(.el-card__body) {
    display: flex;
    flex-direction: column;
    flex: 1;
    overflow: auto;
    .el-table {
      flex: 1;
    }
  }
}
</style>
